package Move;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
//接口是监听键盘键
public class MyPanel extends JPanel implements KeyListener {
    int x=10;
    int y=10;
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillOval(x,y,20,20);
    }
//有字符输出时，该方法触发
    @Override
    public void keyTyped(KeyEvent e) {

    }
//按下某个键时触发
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_DOWN){
            y+=10;
        } else if(e.getKeyCode()==KeyEvent.VK_UP){
            y-=10;
        }
        this.repaint();
    }
//当某个键松开，该方法触发
    @Override
    public void keyReleased(KeyEvent e) {
    }
}
